package edu.pku.sei.modeler.neditor.part;

import java.util.List;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.ConnectionLayer;
import org.eclipse.draw2d.FanRouter;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ShortestPathConnectionRouter;
import org.eclipse.draw2d.XYAnchor;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.NodeEditPart;
import org.eclipse.gef.Request;
import org.eclipse.swt.SWT;

import edu.pku.sei.modeler.model.notation.core.NotationFactory;
import edu.pku.sei.modeler.model.notation.shape.ConnectionTemplate;
import edu.pku.sei.modeler.neditor.model.Diagram;
import edu.pku.sei.modeler.neditor.part.policy.DiagramLayoutEditPolicy;
import edu.pku.sei.modeler.neditor.part.policy.NodeEditPolicy;
import edu.pku.sei.modeler.neditor.ui.NotationEditor;

public class DiagramEditPart extends AbstractMetaElementPart implements
		NodeEditPart {
/*	@Override
	public DiagramEditPart getDiagramPart() {
		return this;
	}*/
	public void refreshStructure(){
		super.refreshStructure();
		this.refreshSourceConnections();
		this.refreshTargetConnections();
	}
/*
	public NotationEditor getAssocatedEditor() {
		return ((Diagram)getModel()).getEditor();
	}*/
/*	public NotationFactory getAssocatedFactory(){
		return ((Diagram)getModel()).getFactoryInstance();
	}*/


	@Override
	protected IFigure createFigure() {
        Figure figure = new FreeformLayer();
        figure.setLayoutManager(new FreeformLayout());
        
        ConnectionLayer connLayer = (ConnectionLayer) getLayer(LayerConstants.CONNECTION_LAYER);
		FanRouter router = new FanRouter();
		router.setSeparation(20);
		router.setNextRouter(new ShortestPathConnectionRouter(figure));
		connLayer.setConnectionRouter(router);
		connLayer.setAntialias(SWT.ON);
        return figure;
	}

	@Override
	protected void createEditPolicies() {
        installEditPolicy(EditPolicy.LAYOUT_ROLE, new DiagramLayoutEditPolicy());
        installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE,new NodeEditPolicy());
	}


	

	@Override
	protected List getModelChildren() {
		return ((Diagram)getModel()).getNodes();
	}

	public ConnectionAnchor getSourceConnectionAnchor(
			ConnectionEditPart connection) {
		return new XYAnchor(((ConnectionTemplate)connection.getModel()).getStartPoint());
	}

	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		//return new XYAnchor(((ReconnectRequest)request).getLocation());
		return null;
	}

	public ConnectionAnchor getTargetConnectionAnchor(
			ConnectionEditPart connection) {
		return new XYAnchor(((ConnectionTemplate)connection.getModel()).getEndPoint());
	}

	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		// TODO Auto-generated method stub
		//return new XYAnchor(new Point(200,100));
		return null;
	}
	protected List getModelSourceConnections() {
		return ((Diagram)getModel()).getMainEditorLinks();
	}

	protected List getModelTargetConnections() {
		return ((Diagram)getModel()).getMainEditorLinks();
	}
	
}
